package cn.sczhyt.modules.szhy.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.sczhyt.modules.szhy.entity.MeetingVoteEntity;
import cn.sczhyt.modules.szhy.service.MeetingVoteService;
import cn.sczhyt.common.utils.PageUtils;
import cn.sczhyt.common.utils.R;


/**
 * 表决表
 *
 * @author luoshijie
 * @email 1169414426@qq.com
 * @date 2020-11-25 10:54:22
 */
@Api(tags = "PC端表决功能")
@RestController
@RequestMapping("szhy/meetingvote")
public class MeetingVoteController {
    @Autowired
    private MeetingVoteService meetingVoteService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("szhy:meetingvote:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = meetingVoteService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 表决结果
     */
    @ApiOperation("表决结果")
    @RequestMapping("/voteCount")
    public R voteCount(@RequestParam String meetingId, @RequestParam String topicsId) {

        JSONObject jsonObject = meetingVoteService.voteCount(meetingId, topicsId);
        //推送表决结果到所有人

        return R.ok().put("data", jsonObject);
    }

    /**
     * 表决列表
     */
    @ApiOperation("表决名单详情")
    @RequestMapping("/voteList")
    public R voteList(@RequestParam String meetingId, @RequestParam String topicsId) {

        List<MeetingVoteEntity> byTopicsId = meetingVoteService.findByTopicsId(meetingId, topicsId);

        return R.ok().put("data", byTopicsId);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("szhy:meetingvote:info")
    public R info(@PathVariable("id") String id) {
        MeetingVoteEntity meetingVote = meetingVoteService.getById(id);

        return R.ok().put("meetingVote", meetingVote);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("szhy:meetingvote:save")
    public R save(@RequestBody MeetingVoteEntity meetingVote) {

        meetingVoteService.save(meetingVote);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("szhy:meetingvote:update")
    public R update(@RequestBody MeetingVoteEntity meetingVote) {
        meetingVoteService.updateById(meetingVote);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("szhy:meetingvote:delete")
    public R delete(@RequestBody String[] ids) {
        meetingVoteService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
